package com.sdhs.paas.sso.sp;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.client.RestTemplate;

import com.sdhs.paas.internet.client.RestClient;
import com.sdhs.paas.internet.login.IUser;
import com.sdhs.paas.internet.login.User;
import com.sdhs.paas.internet.util.StringUtils;

public class UserAction extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4072440582054372678L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		/******************获取IDP登录认证成功后返回值*****************/
		String SMAL = request.getParameter("SAMLResponse");
		if (StringUtils.isEmpty(SMAL)) { //如返回值为空，则进行跳转错误页面处理
			throw new RuntimeException("SAMLResponse已失效");
			//处理错误页面
		}
		
		
		HttpSession session = request.getSession();
		///api/auth/jwt/userinfo
		
		/***************获取IDP登录认证成功后返回的userId*********/
		IUser user = new User();
		user.getUserInfo(SMAL);
		/***************各业务系统（sp端）登录授权开始，业务系统根据自身系统需要进行修改************/
		/**
		 * 在此添加业务系统登录逻辑实现代码，为当前请求用户(userId)授权
		 * 
		 * */
		session.setAttribute("UserLoginInfo", new User());
		/***************各业务系统（sp端）登录授权结束************/
		
		/************sp端登录授权完毕后,跳转到进行IDP授权前保存的url************/
		String RelayState = (String) session.getAttribute("RelayState");
		if (StringUtils.isEmpty(RelayState)) {
			RelayState = "http://" + request.getServerName() + ":"
					+ request.getServerPort() + request.getContextPath();
		}
		response.sendRedirect(RelayState);
	}
	
	
}
